import Vue from 'vue'
import VueResource from 'vue-resource'
Vue.use(VueResource)

export default {
    state: {
        showPlayer: false, // 播放器展示
        showPanelPlayer: false, // 播放详情展示
        isPlay: true, // 播放开关
        audio: {
            songUrl: '',
            imgUrl: 'http://m.kugou.com/v3/static/images/index/logo_kugou.png',
            title: '',
            singer: '',
            currentLength: 0,
            songLength: 0,
            currentFlag: false
        },
        listenCount: 0,
        songList:[],
        songIndex: 0,
    },
    getters: {
        showPlayer: state => state.showPlayer,
        showPanelPlayer: state => state.showPanelPlayer,
        audio: state => state.audio,
        isPlay: state => state.isPlay,
        songList: state => state.songList,
        songIndex: state => state.songIndex,
    },
    mutations: {
        showPlayer: (state, flag) => {
            state.showPlayer = flag;
        },
        setAudio(state, audio) {
            // if (!state.listenCount) {
            //     state.showPlayer = true;  //首次进入应用时不可打开播放详情
            // }
            // state.listenCount++;
            // state.audio = { ...(state.audio), ...audio };
            state.audio =  Object.assign({},state.audio,audio);
        },
        setLrc: (state, lrc)=> {
            // state.audio = {...(state.audio), lrc}
            state.audio =  Object.assign({},state.audio,lrc);
        },
        isPlay: (state, flag) => {
            state.isPlay = flag;
        },
        setSongList: (state, list) => {
            state.songList = list;
        },
        setSongIndex: (state, index) => {
            state.songIndex = index;
        },
        setAudioTime(state, time){
            state.audio.currentLength = time;
        },
        showPanelPlayer(state, flag){
            state.showPanelPlayer = flag;
        },
        setCurrent(state, flag){
            state.audio.currentFlag = flag;
        },
    },
    actions: {
        getSong({ commit, state }, hash) {
            // console.log(hash)
            Vue.http.get(`http://cs003.m2828.com/apis/getKugouSong.php?hash=${hash}`).then(res => {
                var json_obj = res.data;
                // console.log(res)
                if (res.ok) {
                    var songUrl = json_obj.url,
                        imgUrl = json_obj.imgUrl.split('{size}').join('100'),
                        title = json_obj.songName,
                        singer = json_obj.choricSinger,
                        songLength = json_obj.timeLength,
                        currentLength = 0,
                        audio = { songUrl, imgUrl, title, singer, songLength, currentLength };
                    commit('setAudio', audio);
                }
            });
            commit('showPlayer', true);
        },
        getLrc({commit,state}, hash){
        Vue.http.get(`http://cs003.m2828.com/apis/getLrc.php?hash=${hash}`).then(res=> {
            commit('setLrc', res.data);
        })
        },
        changeSong({ dispatch, state }) {
            var list = state.songList;
            var hash = list[state.songIndex].hash;
            dispatch('getSong', hash);
            dispatch('getLrc', hash);
        }
    }
}